﻿
CREATE PROCEDURE [dbo].[TiparesteParteneri]
	@CodUnitate smallint,
	@ParteneriFolositi bit
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT [IDPartener]
      ,[CUI]
      ,p.[Denumire]
      ,b.Denumire as DenumireBanca
      ,[ContBancar]
      ,[Cont]
      ,[CodUnitate]
      ,[TipPartener]
      ,j.Nume AS Judet
      ,l.Nume as Localitate
      ,[Strada]
      ,[Nr]
      ,[AtributFiscal]
      ,[JudetRegComert]
      ,[NrRegComert]
      ,[AnRegComert]
      ,[NumeDelegat]
      ,[SerieAI]
      ,[NrAI]
      ,[MijlocTransport]
      ,[NrMijlocTransport]
      ,p.[CodPostal]
      ,[Telefon]
      ,[Fax]
      ,[ContClient]
      ,[ConditiiDePlata]
      ,t.NUME as Tara
      ,[IDContactDelegat]
      ,[IDMijlocTransport]
      ,[ScadentaFacturiClient]
      ,[ScadentaFacturiFurnizor]
      ,[ScadentaFacturiSfarsitLuna] FROM Parteneri p
      LEFT OUTER JOIN Banci b ON p.IDBanca = b.IdBanca
      LEFT OUTER JOIN Judete j ON p.IDJudet = j.IDJudet
      LEFT OUTER JOIN Tari t ON t.IDTara = p.IDTara
      LEFT OUTER JOIN Localitati l ON l.IDLocalitate = p.IDLocalitate
	WHERE CodUnitate = @CodUnitate
	AND ((@ParteneriFolositi=0) OR EXISTS (SELECT * FROM [Ordonantari] o WHERE o.CodUnitate=@CodUnitate AND o.IDFurnizor=p.IDPartener))
END